package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.framework.admin.dao.UserConfigDao;
import com.wei.czz.framework.admin.entity.UserConfigEntity;
import com.wei.czz.framework.admin.service.UserConfigService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service("userConfigService")
@AllArgsConstructor
public class UserConfigServiceImpl extends ServiceImpl<UserConfigDao, UserConfigEntity> implements UserConfigService {

    private static final Logger log = LoggerFactory.getLogger(UserConfigServiceImpl.class);

    @Override
    public void saveOrEdit(UserConfigEntity userConfig) {
        Long id = userConfig.getId();
        if (Objects.isNull(id)) {

            int count = baseMapper.insert(userConfig);
            log.info("保存用户配置完成。count={}", count);
        } else {
            int count = baseMapper.updateById(userConfig);
            log.info("修改用户配置完成。count={}", count);
        }
    }

    @Override
    public UserConfigEntity get(Long userId, String configKey) {
        LambdaQueryWrapper<UserConfigEntity> userConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userConfigLambdaQueryWrapper.eq(UserConfigEntity::getUserId, userId)
                .eq(UserConfigEntity::getConfigKey, configKey)
                .eq(UserConfigEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        userConfigLambdaQueryWrapper.last(" limit 1");
        List<UserConfigEntity> userConfigList = baseMapper.selectList(userConfigLambdaQueryWrapper);
        log.info("查询用户配置完成。size={}", userConfigList.size());
        if (userConfigList.isEmpty()) {
            return null;
        }
        return userConfigList.get(0);
    }

    @Override
    public List<UserConfigEntity> findList(Long userId) {
        LambdaQueryWrapper<UserConfigEntity> userConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userConfigLambdaQueryWrapper.eq(UserConfigEntity::getUserId, userId)
                .eq(UserConfigEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        //查询
        List<UserConfigEntity> userConfigList = baseMapper.selectList(userConfigLambdaQueryWrapper);
        log.info("查询用户配置成功。userId={} list.size={}", userId, userConfigList.size());
        return userConfigList;
    }
}